package com.mls.fivedistrict.controller.portal;

import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.pojo.NewsReadReport;
import com.mls.fivedistrict.server.NewsReadReportService;
import com.mls.fivedistrict.server.NewsService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by chenxiuxiang on 2018/6/15.
 */
@RestController
@RequestMapping("/api/app")
public class NewsReadReportController extends BaseController {

    @Autowired
    private NewsReadReportService newsReadReportService;
    @Autowired
    private NewsService newsService;

    /**
     * 阅读接口
     * @param newsReadReport
     * @return
     */
    @PostMapping("/v1/newsReadReport")
    public ServiceResponse addReadRecord(@RequestBody NewsReadReport newsReadReport) {
        BeanValidator.check(newsReadReport);
        newsReadReport.setUserId(getUser().getId());
        //快讯id
        Long newsId = newsReadReport.getNewsId();
        //阅读次数加一
        boolean update = newsService.addReadCount(newsId);
        boolean insert = newsReadReportService.addNewsReadReport(newsReadReport);
        if (update) {
            return ServiceResponse.createSuccessResponse("已完成新闻阅读");
        } else {
            return ServiceResponse.createErrorResponse("阅读失败");
        }
    }
}
